\section{Tools}
\label{SecTools}
There are several tools supporting the BDD process. They exist for many different programming languages such as Java, .NET, Ruby or Python. 

First, an briefly overview of tools should be given by categorizing them into different programming languages. Thereafter a tool called \textit{JBehave} shall be covered in depth. 

\subsection{Overview of Tools}
There exists several to perform BDD while using different programming languages. Table \ref{tab-bdd-tools} should give a briefly overview of the tools existing in the world wide web. 

\begin{table}[h]
	\centering
	  \begin{tabular}[h]{|p{2.5cm}|l|l|} \hline
			\specialrule{2pt}{0pt}{0pt}
			\textit{Language} & \textit{Tool} & \textit{Web}  \\
			\specialrule{2pt}{0pt}{0pt}
			\multirow{4}{*}{\parbox{2.2cm}{\textit{Java}}} & JBehave  & http://jbehave.org/\\	
			\cline{2-3}
			 & Concordion & http://www.concordion.org/ \\	
			\cline{2-3}
			 & CoreJet & http://corejet.org/ \\	
			\cline{2-3}
			 & easyb & http://www.easyb.org/ \\	
			\hline	
			\multirow{2}{*}{\parbox{2.2cm}{\textit{Ruby}}} & Cucumber  & http://cukes.info/\\	
			\cline{2-3}
			 & MSpec & https://github.com/machine/machine.specifications \\		
			\hline			
			\multirow{3}{*}{\parbox{2.2cm}{\textit{.NET}}} & SpecUnit  & http://code.google.com/p/specunit-net/\\	
			\cline{2-3}
			 & SpecFlow & http://www.specflow.org \\		
			\cline{2-3}
			 & NBehave & http://nbehave.codeplex.com \\	
			\hline		
			Python & RSpec & http://rspec.info/ \\			
			\hline
		\end{tabular}
		\caption{Overview of BDD tools}
		\label{tab-bdd-tools}		
\end{table}

This overview represents only a very small part of all existing tools. Finally, the development team has to decide for one of these tools while performing BDD in their software development life cycle. Each of them has its advantages and disadvantages. Howeber, a detailed analysis of all these tools would go beyond the scope of this work. Thus, the next section will cover one of these tools in more detail. 

\subsection{Introducing to JBehave by using a "BMI Calculator" Example}
JBehave is a tool or rather a Java framework for supporting BDD in the Java world. JBehave was the first tool which could be used to support the BDD process developed by Dan North in 2003. It supports developers while doing BDD in the software development process with all required basic functionalities. For instance, JBehave is able to handle a grammar for defining BDD keywords. Thus it is possible to map textual descriptions to Java objects.

The following paragraphs should introduce to the general usage of JBehave by using an example called "BMI Calculator". The focus should be on the basic functionalities to give a briefly overview of JBehave. Therefore five different steps can be defined, which represent a whole life cycle of doing BDD by using JBehave: 

\begin{enumerate}
\item{Write a Story}
\item{Map Steps to Java}
\item{Configure Stories}
\item{Run Stories}
\item{View Reports.}
\end{enumerate}

\paragraph{BMI Calculator}{These five steps should be described in the following paragraphs by using an explicit example called "BMI Calculator". The usage of a big case study would go beyond the scope of this work. Thats why a small introducing example has been chosen, which can be described as follows: 

\begin{shaded}
\textit{"\textbf{As} a doctor \textbf{I want to} use a BMI (Body Mass Index) calculator software \textbf{in order to} calculate the BMI of my patients."}
\end{shaded}

This example is aligned to the example used by \cite{Vasiley2012}.

}

\paragraph{1) Write a Story}{The first thing is to define stories, scenarios and steps by using the \textbf{given-when-then} construct as already described in section \ref{process-cycle}. The focus of this story should be closely related to the domain or business and should hide as much technically details as possible. Moreover the story is defined by using the grammar of JBehave. Finally, each story is stored in a separate file with the ending \textbf{.story}. The name of the file should have a clear name, which represents the behavior. 

Listing \ref{lst-bmi-calc} shows the definition of the BMI Calculator example. As first, the narrative of the story is described (lines 1-4) on top of the story-file by using the keywords \textbf{In order}, \textbf{As a} and \textbf{I want to}. This part is optional but it is recommended to describe the story. After that, the scenario should be described by using the \textbf{Scenario} keyword (line 6). Related to the scenario, JBehave makes use of the \textbf{given-when-then} construct (lines 8-11). The \textbf{when} and \textbf{then} keywords contain the variables of the behavior. Here the patient's mass (77kg) and its height (1.75m) as well as the calculated body mass index (25.142...) are the variables. They need to be mapped to Java variables in the next step.

\begin{lstlisting}[label=lst-bmi-calc,caption=Story \texttt{CalculateBMI.story}]
Narrative: 
In order to calculate the BMI of my patients
As a doctor
I want to use a BMI calculator software

Scenario: BMI Calculation

Given a body mass index calculator
When a doctor populates a patients health record 
  with mass 77 kg and height 1.75 m
Then patients body mass index is 25.14285659790039
\end{lstlisting}

}

\paragraph{2) Map Steps to Java}{The next task is to map the defined story to an appropriate Java class. Thereby JBehave maps the textual steps defined by the \textbf{given-when-then} construct to different Java methods. Listing \ref{lst-bmi-steps} shows the Java class \texttt{MetricBMICalculatorSteps}, which contains the actual mapping of story to Java methods. 

\begin{lstlisting}[label=lst-bmi-steps,caption=\textit{BMI Calculator} steps]
public class MetricBMICalculatorSteps {

	private MetricBMICalculator bmiCalculator;
	private BodyMassIndex bmi;
	
	public MetricBMICalculatorSteps(){}
	
	@Given("a body mass index calculator")
	public void initStock(){
		bmiCalculator = new MetricBMICalculator();		
	}
	
	@When("a doctor populates a patients health record 
					with mass $weight kg and height $height m")
	public void populateHealthRecord(float weight, float height){
		bmi = bmiCalculator.calculate(weight, height);
	}
	
	@Then("patients body mass index is $bmi")
	public void checkBmi(@Named("bmi") double bmiValue){
		Assert.assertEquals(bmiValue, bmi.getBmi());
	}
}
\end{lstlisting}

This class is a common Java class (POJO) and does not inherit from any other class. On line 8,13 and 19 the different steps (given, when, then) are defined. Each step is defined by a Java annotation for instance the \textbf{given} step is annotated with the \texttt{@Given} annotation (line 8). Moreover it can be seen, that at least the \texttt{@When} and \texttt{@Then} contains some kind of variables (\texttt{\$weight, \$height} and \texttt{\$bmi}). These variables are mapped to the defined values in the story and are converted into parameters of the appropriate method within the class. Moreover line 20 contains an annotation called \texttt{@Named}. This annotation can be used if there are some kind of conflicts between the variables in the defined step and the attributes of the class. In this example, the \texttt{\$bmi} variable of the step is conflicted with the private \texttt{bmi} attribute of the class. Thus the \texttt{@Named(bmi)} annotation is used to grab the variable of the step and rename it to \texttt{bmiValue} (line 20).
}

\paragraph{3) Configure Stories}{The next step is to setup a configuration of the behavior test(s) in order to make them runnable. One option is to extend the class \texttt{JUnitStories} and to set the required configurations. Listing \ref{lst-bmi-config} shows such a configuration for a story. 

\begin{lstlisting}[label=lst-bmi-config,caption=\textit{BMI Calculator} story configuration]
public class BMICalculatorMultipleStories extends JUnitStories {

	@Override
	public Configuration configuration() {
		return new MostUsefulConfiguration().useStoryLoader(
				new LoadFromClasspath(this.getClass()))
				.useStoryReporterBuilder(
						new StoryReporterBuilder().withCodeLocation(
								CodeLocations.codeLocationFromClass(this
										.getClass())).withFormats(
								Format.CONSOLE, Format.HTML));
	}

	@Override
	public List<CandidateSteps> candidateSteps() {
		return new InstanceStepsFactory(configuration(),
				new MetricBMICalculatorSteps()).createCandidateSteps();
	}

	@Override
	protected List<String> storyPaths() {
		return new StoryFinder().findPaths(
				CodeLocations.codeLocationFromClass(this.getClass()),
				"**/CalculateBMI*.story", "");
	}
}
\end{lstlisting}

In JBehave exists a lot of possibilities for configuring the tests. Due to the scope of this work, only the main configurations should be covered. 

Line 3-12 are responsible for the configuration of the output format of the report. For instance it is possible to generate a report on the \textit{console}, as a \textit{txt} file or as an advanced \textit{HTML} report. At this example only \textit{console} and \textit{HTML} format has been chosen (line 11). Line 14-18 configures the required \texttt{MetricBMICalculatorSteps} class containing the different steps (cf. listing \ref{lst-bmi-steps}). Finally it is required to configure the path, where the story can be found (line 20-25). The path can be a regular expression as shown in line 24. The \texttt{StoryFinder} (line 22) will load all stories, which are located at the given path. In this case all stories beginning with the name \textit{CalculateBMI} and ending with the name \textit{.story} will be loaded.
}

\paragraph{4) Run Stories}{JBehave is designed to be runnable from different environments. For instance, JBehave tests can be executed as a common JUnit test. This can be done from the console or from any kind of IDE supporting JUnit. Furthermore JBehave tests can be triggered from an ANT or Maven scripts. 

In the example of the BMI calculator, the class \texttt{BMICalculatorMultipleStories} (cf. listing \ref{lst-bmi-config}) can be simply executed as JUnit test in the Eclipse IDE. 
}

\paragraph{5) View Reports}{The previous step did the most important part of the JBehave BDD process: it generated the reports of the executed tests. This is an essential part of BDD, because the reports represents the outcome of the defined test cases / stories. As already mentioned in the third step (\textit{Configure Stories}), there are many different options of how to generate a report. This is done by the so called \texttt{StoryReporter} (cf. line 8, listing \ref{lst-bmi-config}). Figure \ref{fig-bmi-calc-report} is an example for an outcome of the \textit{CalculateBMI} story. The part below the scenario is written in green, which marks the test as successful. There are also several other pages generated by JBehave, which shows some kind of statistics about the complete test run. This should be skipped at this point.

\begin{figure}[h]
\begin{center}
\includegraphics[scale=0.8]{images/bmi-calc-report.jpg}
\end{center}
\caption{JBehave report of the \textit{BMI Calculator Story}}
\label{fig-bmi-calc-report}
\end{figure}

}



\paragraph{Conclusion}{The JBehave framework is very flexible regarding the BDD process. Beside its basic functionalities it provides many useful extensions for instance for the mapping of stories. However the installation and configuration of JBehave seems to be complicated compared to other frameworks like JUnit. This belongs to the documentation of JBehave, which lacks on some parts like the installation. Anyway, once JBehave is configured and after some practical implementations, the usage of JBehave should fit for an experienced developer. Nevertheless, the authors think that JBehave can be successfully used in projects practicing the BDD methodology.}


